package org.scboy.dbms.loads;

import org.scboy.coobi.sql.Coobi;
import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.dao.RequestDesignDao;
import org.scboy.utils.caches.DataCache;
import org.scboy.utils.caches.dbms.entity.DesignEntity;
import org.scboy.utils.entitys.coobi.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

/**
 * 字典缓存管理
 * @author 杨浩
 */
@Configuration
@EnableCaching
@AutoConfigureAfter(value = {Coobi.class,EntityFactory.class})
public class LoadRequestDesign implements DataCache<DesignEntity>{

	@Autowired
	EntityFactory factory;

	Log log =LogFactory.get();
	
	/**
	 * 获取缓存数据
	 * @return
	 */
	@Override
	@Cacheable(value="DBMS_REQUEST_DESIGN",key="#result.appName + '['+#result.code+']'",unless="#result == null")
	public DesignEntity get(String id) {
		RequestDesignDao dao=factory.getDao(RequestDesignDao.class);
		Model dic=dao.get(Model.create().add("id", id));
		return dic.toEntity(DesignEntity.class);
	}
	
	/**
	 * 根据ID更新缓存
	 * @return
	 */
	@Override
	@CachePut(value="DBMS_REQUEST_DESIGN",key = "#result.appName + '['+#result.code+']'",unless="#result == null")
	public DesignEntity update(DesignEntity entity) {
		return entity;
	}
	
	/**
	 * 根据ID更新缓存
	 * @param id
	 * @return
	 */
	@Override
	@CachePut(value="DBMS_REQUEST_DESIGN",key = "#result.appName + '['+#result.code+']'",unless="#result == null")
	public DesignEntity updateByKey(String id) {
		return get(id);
	}
	
	/**
	 * 移除缓存
	 * @param id
	 * @return
	 */
	@Override
	@CacheEvict(value="DBMS_REQUEST_DESIGN",key = "#result.appName + '['+#result.code+']'")
	public DesignEntity removeCache(String id) {
		return get(id);
	}
	
	/**
	 * 清空所有缓存
	 * @return
	 */
	@Override
	@CacheEvict(value="DBMS_REQUEST_DESIGN",allEntries = true)
	public void removeAll() {}
}
